//
//  QDSelectSicknessController.m
//  simulateQuickDoctor
//
//  Created by 梁家伟 on 17/3/24.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDSelectSicknessController.h"
#import "QDBaseTableViewCell.h"
#import "QDNetworkTool.h"

@interface QDSelectSicknessController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,weak)UITableView* sicknessList;
@end

@implementation QDSelectSicknessController{
    
    NSMutableArray* _sicknessData;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _sicknessData = [NSMutableArray array];
    
    [self loadData];
}

static NSString* sickCell = @"sickness";

- (void)setupUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITableView* sicknessList = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    sicknessList.dataSource =self;
    sicknessList.delegate = self;
    
    [self.view addSubview:sicknessList];
    
    [sicknessList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    _sicknessList = sicknessList;
    
    
    [sicknessList registerClass:[QDBaseTableViewCell class] forCellReuseIdentifier:sickCell];
  
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return _sicknessData.count;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSUInteger ci2_id = [_sicknessData[indexPath.row][@"ci2_id"] integerValue];
    
    NSString* ci3_name =_sicknessData[indexPath.row][@"ci3_name"];
    
    NSUInteger ci3_id = [_sicknessData[indexPath.row][@"ci3_id"] integerValue];
    
//    NSLog(@"ci2_id = %zd,\nci3_id = %zd",ci2_id,ci3_id);
    
    self.callBackBlock(ci2_id,ci3_id,ci3_name);
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    QDBaseTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:sickCell forIndexPath:indexPath];
    
    
    cell.textLabel.text = _sicknessData[indexPath.row][@"ci3_name"];;
    
    
    return cell;
}


- (void)loadData{
    
    NSString* requestURLString = @"http://iosapi.itcast.cn/doctor/searchCI3List.json.php";
    
    
     NSDictionary *postParams = @{@"page":@1,@"page_size":@15,@"ci1_id":@(_cid),@"keyword":@""};

    
    
    QDNetworkTool* tool = [QDNetworkTool sharedTool];
    
    tool.requestSerializer = [AFJSONRequestSerializer serializer];
    
    [QDBannerTools pp_BannerTitle:@"正在加载.." andTime:0.3];
    
    [tool requestWithHTTPMethod:@"POST" URLString:requestURLString parameters:postParams CallBack:^(id responseObj) {
        
        if(responseObj != nil){
            
            NSString* result = responseObj[@"msg"]; //FAIL
            
            if([result isEqualToString:@"OK"]){
                
                NSArray* dataArr = responseObj[@"data"];
                
//                dataArr = [dataArr sortedArrayUsingComparator:^NSComparisonResult(NSDictionary*  _Nonnull obj1, NSDictionary*  _Nonnull obj2) {
//                    return [obj1[@"ci2_id"] compare:obj2[@"ci2_id"]];
//                }];
                
//                NSLog(@"%@",dataArr);
                
                [_sicknessData addObjectsFromArray:dataArr];
                
                NSLog(@"count = %zd",_sicknessData.count);
                
                [_sicknessList reloadData];
                
            }else{
                NSLog(@"请求参数错误");
            }
            
        }else{
            NSLog(@"网络请求错误");
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
